﻿using System;
using System.Linq;
using Sustainalytics.Reporting.Framework;
using Foxit.PDF.PageElements;
using Foxit.PDF;

namespace Sustainalytics.Governance.Reporting
{
    public class SourcesSection: PdfReportSectionGenerator
    {

        public override void ComposeSectionContent()
        {
            const string sectionTitle = "Sources";
            const float sectionTitleHeight = 22f;
            const float titleFontSize = 16f;
            const float top = 20f;

            var testStyle = new FormattedTextAreaStyle(Fonts.CalibriLightFontFamily, 8f, false)
            {
                Paragraph = { Align = TextAlign.Left, Indent = 0, SpacingAfter = 0, AllowOrphanLines = false }
            };
            var numberStyle = new FormattedTextAreaStyle(Fonts.CalibriLightFontFamily, 8f, false)
            {
                Paragraph = { Align = TextAlign.Right, Indent = 0, SpacingAfter = 0, AllowOrphanLines = false }
            };


            var width = _report.PageMaxWidth;
            var height = _report.PageMaxHeight - 25f;

            var table = GetDefaultTable(_report.LeftMargin, top, width, height);


            table.Columns.Add(width*0.4f);
            table.Columns.Add(width*0.6f);

            var row = table.Rows.Add(sectionTitleHeight);


            var title = string.Format("<p><font color='FFFFFF'><b>{0}</b></font></p>", sectionTitle);
            var cell = row.Cells.Add(new FormattedTextArea(title, 5f, 1f, width, titleFontSize + 2f, Fonts.CalibriFontFamily, titleFontSize, false), 2);
            cell.BackgroundColor = Colors.SectionColor;
            cell.VAlign = VAlign.Center;

            row = table.Rows.Add(EmptyRowHeight);
            row.Cells.Add(string.Empty);


            if (_dataSource.Sources != null && _dataSource.Sources.Any())
            {
                foreach (var source in _dataSource.Sources.OrderBy(s=> s.Text))
                {
                    
                    var sourceText = string.Format("<p>{0}</p>", source.Text);
                    var formattedTextArea = new FormattedTextArea(sourceText, 0.0f, 5.0f, width * 0.4f, 100f, testStyle);
                    var sourceTextHeight = formattedTextArea.GetRequiredHeight() + 5f;



                    var indicatorsNumbers = source.IndicatorsNumbers
                        .Select(i => i.Trim().Substring(3))
                        .OrderBy(i => Convert.ToInt32(i.Substring(2))).ToList();

                    var numbers = string.Format("<p>{0}</p>", string.Join(", ", indicatorsNumbers.Select(i => "CG." + i).OrderBy(i => i)));
                    formattedTextArea = new FormattedTextArea(numbers, 0.0f, 5.0f, width * 0.6f, 100f, numberStyle);
                    var numbersHeight = formattedTextArea.GetRequiredHeight() + 5f;


                    var requiredHeight = Math.Max(sourceTextHeight, numbersHeight);
                    row = table.Rows.Add(requiredHeight);

                    formattedTextArea = new FormattedTextArea(sourceText, 0.0f, 5.0f, width * 0.4f, sourceTextHeight, testStyle);

                    row.Cells.Add(formattedTextArea);

                    formattedTextArea = new FormattedTextArea(numbers, 0.0f, 5.0f, width * 0.6f, numbersHeight, numberStyle);

                    row.Cells.Add(formattedTextArea);
                }
            }

            do
            {
                var page = _report.NewPage();
                _report.Pages.Add(page);
                page.Elements.Add(table);
                table = table.GetOverflowRows();

            } while (table != null);
        }


    }
}
